package com.lemon.core.conf.core;


import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

public class ZookeeperResourcerParser extends AbstractSimpleBeanDefinitionParser {

    private enum InitializeBy {
        CONSTRUCTOR_ARGS, LOCAL_FILE
    }

    @Override
    protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
        InitializeBy initializeBy = InitializeBy.valueOf(element.getAttribute("initializeBy"));
        switch (initializeBy) {
            case CONSTRUCTOR_ARGS:
                builder.addConstructorArgValue(element.getAttribute("server"))
                        .addConstructorArgValue(element.getAttribute("znodes"))
                        .addConstructorArgValue(ZookeeperResource.PingCmd.valueOf(element.getAttribute("pingCmd")))
                        .addConstructorArgValue(Boolean.valueOf(element.getAttribute("regression")))
                        .addConstructorArgValue(ZookeeperResource.OnConnectionFailed.valueOf(element.getAttribute("onConnectionFailed")))
                        .addConstructorArgValue(ZookeeperResource.ReloadContext.valueOf(element.getAttribute("reloadContext")));
                break;
            case LOCAL_FILE:
                break;
        }
    }

    @Override
    protected Class<ZookeeperResource> getBeanClass(Element element) {
        return ZookeeperResource.class;
    }

}
